package com.example.peibei.ui.activity;

import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.bean.MyFollow;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.FollowPresenter;
import com.example.peibei.service.presenter.MyFansPresenter;
import com.example.peibei.service.presenter.MyFollowPresenter;
import com.example.peibei.ui.adapter.MyFansAdapter;
import com.example.peibei.ui.adapter.MyFollowAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyFansListActivity extends WDActivity {
    @BindView(R.id.rv_follow)
    RecyclerView rv_follow;
    @BindView(R.id.tv_title)
    TextView tv_title;

    private SPUtils spUtils;
    private String token;
    private MyFansPresenter myFollowPresenter;
    private List<MyFollow.RecordsDTO> list=new ArrayList<>();
    private MyFansAdapter myFollowAdapter;
    private FollowPresenter followPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_follow;
    }

    @Override
    protected void initView() {
        tv_title.setText("我的粉丝");
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        rv_follow.setLayoutManager(new LinearLayoutManager(this));
        myFollowAdapter = new MyFansAdapter(list);
        rv_follow.setAdapter(myFollowAdapter);
        myFollowPresenter = new MyFansPresenter(new SearchCall());
        followPresenter = new FollowPresenter(new FollowCall());
        myFollowAdapter.addChildClickViewIds( R.id.ll_followed);
        myFollowAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.ll_followed) {
                MyFollow.RecordsDTO item = (MyFollow.RecordsDTO) adapter.getData().get(position);
                String id = item.getMid();
                followPresenter.reqeust(1,"Bearer " + token, id);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        myFollowPresenter.reqeust(true, "Bearer " + token);
    }

    class FollowCall implements DataCall<Dynamic> {

        @Override
        public void success(Dynamic data, Object... args) {
            myFollowPresenter.reqeust(true, "Bearer " + token);
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    class SearchCall implements DataCall<MyFollow> {

        @Override
        public void success(MyFollow data, Object... args) {
            //添加列表并刷新
            if (myFollowPresenter.getPage() == 1) {
                list.clear();
            }
            myFollowAdapter.setNewInstance(data.getRecords());
            myFollowAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @Override
    protected void destoryData() {

    }
}
